﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CapaDeEntidades;
using System.Data;

namespace CapaDatos
{
    public class ReservaDao
    {
        public static string connectionstring = "Initial Catalog=Biblioteca;Data Source=localhost\\SQLEXPRESS;Integrated Security=SSPI;";

        public static bool RegistrarReserva(Reserva reserva)
        {
            bool respu = false;
            SqlConnection conn = new SqlConnection(connectionstring);

            try
            {
                int codLibro = 0;
                int socio = 0;

                /* if (reserva.Libro.IdLibro > 0)
                 {
                     codLibro = reserva.Libro.IdLibro;
               
                 }

                 if (reserva.Socio.Codigo != null)
                 {
                     socio = reserva.Socio.Codigo;
                 }*/

#warning PREGUNTAR ACA CUANDO ES LA FECHA DE FIN DE RESERVA OSEA CUANDO CADUCA PARA SABER QUE INSERTO O NO EN LA DB
                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_CrearReserva", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@idCodigoLibro", codLibro);
                cmd.Parameters.AddWithValue("@idCodigoSocio", socio);
                cmd.Parameters.AddWithValue("@fechaReserva", reserva.FechaReserva);
                cmd.Parameters.AddWithValue("@alta_Reserva", respu);
                cmd.Parameters["@alta_Reserva"].Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                respu = Convert.ToBoolean(cmd.Parameters["@alta_Reserva"].Value);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
            return respu;
        }


        public static DataSet ObtenerReservasPorLibro(Libro libro)
        {
            SqlConnection conn = new SqlConnection(connectionstring);
            DataSet ds = new DataSet();
            try
            {
                int idLibro = 0;

                if (libro.IdLibro != null)
                {
                    idLibro = libro.IdLibro;
                }

                SqlCommand cmd = new SqlCommand("sp_VerReservasPorLibro", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@idLibro", idLibro);

                //LLENO EL DATASET
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
            return ds;
        }


        public static DataSet ObtenerReservasPorSocio(Reserva reserva)
        {
            SqlConnection conn = new SqlConnection(connectionstring);
            DataSet ds = new DataSet();
            try
            {
                int idSocio = 0;

                if (reserva.Socio.Codigo != null)
                {
                    idSocio = reserva.Socio.Codigo;
                }

                SqlCommand cmd = new SqlCommand("sp_ObtenerReservasPorSocio", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@idSocio", idSocio);

                //LLENO EL DATASET
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
            return ds;
        }





    }
}
